import axios from 'axios';
import { constants } from './index'
import { fromJS } from 'immutable'

const getAllHomeData = (result) => ({
    type: constants.GET_CHANGE_HOME,
    topicList: fromJS(result.topicList),
    articleList: fromJS(result.articleList),
    authorsList: fromJS(result.authorsList)
});

const getMoreDataList = (result) => ({
    type: constants.GET_MORE_DATA,
    moreList: fromJS(result.articleList)
})

export const getMoreData = () => {
    return (dispatch) => {
        axios.get('/api/moreData.json').then((res) => {
            const result = res.data.data;
            dispatch(getMoreDataList(result))
        })
    }   
}

export const toggleShowScroll = (showScroll) => ({
    type: constants.TOGGLE_SHOW_STATUS,
    showScroll
})

export const getHomeData = () => {
    return (dispatch) => {
        axios.get('/api/home.json').then((res) => {
            const result = res.data.data;
            dispatch(getAllHomeData(result));
        })
    }
}